import { cloneDeep } from 'lodash-es'
import { http } from '@/utils/http'
import { ElMessage, ElMessageBox } from 'element-plus'
import config from '@/config'

import _ from 'lodash-es'

export const useFananStore = () => {
    const router = useRouter()
    const state = reactive({
        chemelist: [],
        loadingtable: false,
        activetabs: 0,
        page: {
            page: 1,
            page_size: 20,
            total: 0
        }
    })

    const getschemelist = () => {
        if (state.activetabs == 0) {
            http
                .get(`/scheme/scheme_list?page=${state.page.page}&page_size=${state.page.page_size}`)
                .then(res => {
                    console.log(res);
                    state.chemelist = res.data.list
                    state.loadingtable = false
                    state.page.total = res.data.total
                })
                .catch(() => {
                    state.loadingtable = false
                })
        } else {
            http
                .get(`/scheme/copy_scheme_list?page=${state.page.page}&page_size=${state.page.page_size}`)
                .then(res => {
                    console.log(res);
                    state.chemelist = res.data.list
                    state.loadingtable = false
                    state.page.total = res.data.total
                })
                .catch(() => {
                    state.loadingtable = false
                })
        }

    }

    const handleSizeChange = () => {
        state.loadingtable = true
        getschemelist()
    }

    const deletefanan = (row) => {
        if (state.activetabs == 0) {
            http
                .delete(`/scheme/delete_scheme?scheme_code=${row.scheme_code}`)
                .then(res => {
                    ElMessage({
                        message: res.msg,
                        type: 'success',
                    })
                    state.loadingtable = true
                    getschemelist()
                })
                .finally(() => {
                })
        } else {
            http
                .delete(`/scheme/delete_copy_scheme?scheme_code=${row.scheme_code}`)
                .then(res => {
                    ElMessage({
                        message: res.msg,
                        type: 'success',
                    })
                    state.loadingtable = true
                    getschemelist()
                })
                .finally(() => {
                })
        }


    }
    const changestatus = (row) => {
        if (state.activetabs == 0) {
            http
                .post(`/scheme/update_scheme_status?`, row)
                .then(res => {
                    ElMessage({
                        message: res.msg,
                        type: 'success',
                    })
                })
                .finally(() => {
                })
        } else {
            http
                .post(`/scheme/update_copy_scheme_status?`, row)
                .then(res => {
                    ElMessage({
                        message: res.msg,
                        type: 'success',
                    })
                })
                .finally(() => {
                })
        }

    }
    const openschemegendan = () => {
        router.replace(`schemeGendan`)
    }
    const openscheme = () => {
        router.replace(`scheme`)
    }

    const clicktabs = (value) => {
        state.activetabs = value
        initData()
    }




    onMounted(async () => {
        initData()

    })

    onUnmounted(() => {
    })


    async function initData() {
        state.loadingtable = true
        getschemelist()

    }


    return {
        state,
        handleSizeChange,
        deletefanan,
        changestatus,
        openscheme,
        openschemegendan,
        clicktabs

    }
}
